import 'package:app/utils/utils.dart';
import 'package:flutter/material.dart';

class SwiperView extends StatefulWidget {
  final String name;
  final int targetTimestamp;
  const SwiperView({super.key, this.name = "", this.targetTimestamp = 1});
  @override
  State<SwiperView> createState() => _SwiperViewState();
}

class _SwiperViewState extends State<SwiperView> {
  String computerDate(int targetTimestamp) {
    if (targetTimestamp == 1) {
      return "";
    }
    return "目标日:${Utils.formatDate(targetTimestamp, 'yyyy-MM-dd e')}";
  }

  @override
  Widget build(BuildContext context) {
    final dateText = computerDate(widget.targetTimestamp);
    if (dateText.isEmpty) {
      return Container(
        decoration: BoxDecoration(color: Colors.white),
        width: double.infinity,
        height: 100,
      );
    }

    return Container(
      decoration: BoxDecoration(color: Colors.white),
      width: double.infinity,
      height: 100,
      child: Row(
        children: [
          // 左边 (3份)
          Expanded(
            flex: 3,
            child: Container(
              padding: EdgeInsets.only(left: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.name,
                    style: TextStyle(fontSize: 25),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  Text(
                    dateText,
                    style: TextStyle(fontSize: 10),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          ),
          // 中间 (2份)
          Expanded(
            flex: 2,
            child: LayoutBuilder(
              builder: (context, constraints) {
                final maxWidth = constraints.maxWidth;
                var dayObj = Utils.calculateTimeDifference2(
                  widget.targetTimestamp,
                  DateTime.now().millisecondsSinceEpoch,
                );
                var text=dayObj.days;
                if(int.parse(text)==0){
                  text="小于一天";
                }
                final textStyle = TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 35,
                );

                // 计算适应的最大字体大小
                var fontSize = 25.toDouble();
                var minFontSize = 15.toDouble();

                // 使用 TextPainter 测量文本宽度
                final textPainter = TextPainter(
                  text: TextSpan(
                    text: text,
                    style: textStyle.copyWith(fontSize: fontSize),
                  ),
                  maxLines: 1,
                  textDirection: TextDirection.ltr,
                );
                textPainter.layout(maxWidth: maxWidth);

                // 如果超出最大宽度，逐步减小字体大小
                while (textPainter.didExceedMaxLines &&
                    fontSize > minFontSize) {
                  fontSize--;
                  textPainter.text = TextSpan(
                    text: text,
                    style: textStyle.copyWith(fontSize: fontSize),
                  );
                  textPainter.layout(maxWidth: maxWidth);
                }

                return Center(
                  child: Text(
                    text,
                    style: textStyle.copyWith(fontSize: fontSize),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                );
              },
            ),
          ),
          // 右边 (1份)
          Expanded(
            flex: 1,
            child: Center(
              child: Container(
                height: 24,
                width: 40,
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Center(
                  child: const Text(
                    "Days",
                    style: TextStyle(color: Colors.white, fontSize: 12),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
